/*
 * Copyright (c) 2020 Stephanos Ioannidis <root@stephanos.io>
 * Copyright (C) 2010-2020 ARM Limited or its affiliates. All rights reserved.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr/ztest.h>
#include <zephyr/kernel.h>
#include <stdlib.h>
#include <arm_math.h>
#include "../../common/benchmark_common.h"

#define PATTERN_LENGTH	(256)

static const q7_t input1[PATTERN_LENGTH] = {
	0x17, 0xCC, 0x33, 0xF2, 0x14, 0x01, 0x2A, 0xF2,
	0xEC, 0x22, 0x01, 0xF7, 0x2A, 0xB5, 0xCF, 0xED,
	0x2F, 0xE8, 0xCB, 0xE8, 0xF2, 0x08, 0xC6, 0xF2,
	0x34, 0x2F, 0xE1, 0xEF, 0xE8, 0xF1, 0xC1, 0xEF,
	0x3B, 0x23, 0x07, 0x01, 0xFE, 0x07, 0x03, 0xF7,
	0x45, 0xF5, 0xE9, 0xF5, 0x0F, 0x13, 0xF5, 0xF2,
	0x06, 0x1C, 0xF4, 0xFB, 0x12, 0xAF, 0x09, 0x0C,
	0xE8, 0x03, 0xF2, 0x10, 0x2D, 0xD3, 0x16, 0xF5,
	0x32, 0x0C, 0x59, 0xDA, 0x0C, 0x1C, 0xB5, 0x49,
	0x23, 0x05, 0xA1, 0x21, 0x1B, 0x1E, 0xCE, 0xC1,
	0xE0, 0x58, 0x22, 0x01, 0xDB, 0x05, 0x06, 0x30,
	0xCF, 0xCC, 0x0D, 0xA1, 0x38, 0x35, 0xFE, 0x19,
	0xD5, 0x01, 0x15, 0x37, 0x10, 0xDC, 0xF4, 0xD0,
	0x09, 0x0F, 0x1C, 0xA3, 0xCA, 0xF7, 0x29, 0x19,
	0x10, 0xF5, 0xFB, 0xD8, 0x0A, 0xA4, 0xD3, 0x08,
	0x44, 0xDF, 0x2F, 0x5E, 0xFA, 0xFC, 0x48, 0x0B,
	0xFA, 0xB9, 0x43, 0xF3, 0xCC, 0x6A, 0x51, 0x0B,
	0xF8, 0xC6, 0xD2, 0xE0, 0x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, 0x04, 0xF0, 0x03, 0xFE, 0x04, 0xD3,
	0x1E, 0xD2, 0x0B, 0x08, 0xCD, 0xC1, 0xDF, 0xFE,
	0xF7, 0xDB, 0xD0, 0x03, 0x4F, 0xE7, 0x0D, 0x1A
	};

ZTEST(basicmath_q7_benchmark, test_benchmark_vec_add_q7)
{
	uint32_t irq_key, timestamp, timespan;
	q7_t *output;

	/* Allocate output buffer */
	output = malloc(PATTERN_LENGTH * sizeof(q7_t));
	zassert_not_null(output, "output buffer allocation failed");

	/* Begin benchmark */
	benchmark_begin(&irq_key, &timestamp);

	/* Execute function */
	arm_add_q7(input1, input2, output, PATTERN_LENGTH);

	/* End benchmark */
	timespan = benchmark_end(irq_key, timestamp);

	/* Free output buffer */
	free(output);

	/* Print result */
	TC_PRINT(BENCHMARK_TYPE " = %u\n", timespan);
}

ZTEST(basicmath_q7_benchmark, test_benchmark_vec_sub_q7)
{
	uint32_t irq_key, timestamp, timespan;
	q7_t *output;

	/* Allocate output buffer */
	output = malloc(PATTERN_LENGTH * sizeof(q7_t));
	zassert_not_null(output, "output buffer allocation failed");

	/* Begin benchmark */
	benchmark_begin(&irq_key, &timestamp);

	/* Execute function */
	arm_sub_q7(input1, input2, output, PATTERN_LENGTH);

	/* End benchmark */
	timespan = benchmark_end(irq_key, timestamp);

	/* Free output buffer */
	free(output);

	/* Print result */
	TC_PRINT(BENCHMARK_TYPE " = %u\n", timespan);
}

ZTEST(basicmath_q7_benchmark, test_benchmark_vec_mult_q7)
{
	uint32_t irq_key, timestamp, timespan;
	q7_t *output;

	/* Allocate output buffer */
	output = malloc(PATTERN_LENGTH * sizeof(q7_t));
	zassert_not_null(output, "output buffer allocation failed");

	/* Begin benchmark */
	benchmark_begin(&irq_key, &timestamp);

	/* Execute function */
	arm_mult_q7(input1, input2, output, PATTERN_LENGTH);

	/* End benchmark */
	timespan = benchmark_end(irq_key, timestamp);

	/* Free output buffer */
	free(output);

	/* Print result */
	TC_PRINT(BENCHMARK_TYPE " = %u\n", timespan);
}

ZTEST(basicmath_q7_benchmark, test_benchmark_vec_abs_q7)
{
	uint32_t irq_key, timestamp, timespan;
	q7_t *output;

	/* Allocate output buffer */
	output = malloc(PATTERN_LENGTH * sizeof(q7_t));
	zassert_not_null(output, "output buffer allocation failed");

	/* Begin benchmark */
	benchmark_begin(&irq_key, &timestamp);

	/* Execute function */
	arm_abs_q7(input1, output, PATTERN_LENGTH);

	/* End benchmark */
	timespan = benchmark_end(irq_key, timestamp);

	/* Free output buffer */
	free(output);

	/* Print result */
	TC_PRINT(BENCHMARK_TYPE " = %u\n", timespan);
}

ZTEST(basicmath_q7_benchmark, test_benchmark_vec_negate_q7)
{
	uint32_t irq_key, timestamp, timespan;
	q7_t *output;

	/* Allocate output buffer */
	output = malloc(PATTERN_LENGTH * sizeof(q7_t));
	zassert_not_null(output, "output buffer allocation failed");

	/* Begin benchmark */
	benchmark_begin(&irq_key, &timestamp);

	/* Execute function */
	arm_negate_q7(input1, output, PATTERN_LENGTH);

	/* End benchmark */
	timespan = benchmark_end(irq_key, timestamp);

	/* Free output buffer */
	free(output);

	/* Print result */
	TC_PRINT(BENCHMARK_TYPE " = %u\n", timespan);
}

ZTEST(basicmath_q7_benchmark, test_benchmark_vec_offset_q7)
{
	uint32_t irq_key, timestamp, timespan;
	q7_t *output;

	/* Allocate output buffer */
	output = malloc(PATTERN_LENGTH * sizeof(q7_t));
	zassert_not_null(output, "output buffer allocation failed");

	/* Begin benchmark */
	benchmark_begin(&irq_key, &timestamp);

	/* Execute function */
	arm_offset_q7(input1, 1.0, output, PATTERN_LENGTH);

	/* End benchmark */
	timespan = benchmark_end(irq_key, timestamp);

	/* Free output buffer */
	free(output);

	/* Print result */
	TC_PRINT(BENCHMARK_TYPE " = %u\n", timespan);
}

ZTEST(basicmath_q7_benchmark, test_benchmark_vec_scale_q7)
{
	uint32_t irq_key, timestamp, timespan;
	q7_t *output;

	/* Allocate output buffer */
	output = malloc(PATTERN_LENGTH * sizeof(q7_t));
	zassert_not_null(output, "output buffer allocation failed");

	/* Begin benchmark */
	benchmark_begin(&irq_key, &timestamp);

	/* Execute function */
	arm_scale_q7(input1, 0x45, 1, output, PATTERN_LENGTH);

	/* End benchmark */
	timespan = benchmark_end(irq_key, timestamp);

	/* Free output buffer */
	free(output);

	/* Print result */
	TC_PRINT(BENCHMARK_TYPE " = %u\n", timespan);
}

ZTEST(basicmath_q7_benchmark, test_benchmark_vec_dot_prod_q7)
{
	uint32_t irq_key, timestamp, timespan;
	q31_t output;

	/* Begin benchmark */
	benchmark_begin(&irq_key, &timestamp);

	/* Execute function */
	arm_dot_prod_q7(input1, input2, PATTERN_LENGTH, &output);

	/* End benchmark */
	timespan = benchmark_end(irq_key, timestamp);

	/* Print result */
	TC_PRINT(BENCHMARK_TYPE " = %u\n", timespan);
}

ZTEST_SUITE(basicmath_q7_benchmark, NULL, NULL, NULL, NULL, NULL);
